<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input v-model="listQuery.outTradeNo" class="input-width" placeholder="订单编号"></el-input>
          </el-form-item>
          <el-form-item label="收货人：">
            <el-input v-model="listQuery.keyword" class="input-width" placeholder="收货人姓名/手机号码"></el-input>
          </el-form-item>
          <el-form-item label="快递单号：">
            <el-input v-model="listQuery.express" class="input-width" placeholder="请输入快递单号"></el-input>
          </el-form-item>
          <el-form-item label="提交时间：">
            <el-date-picker
              class="input-width"
              v-model="listQuery.createTime"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="订单状态：">
            <el-select v-model="listQuery.state" class="input-width" placeholder="全部">
              <el-option v-for="item in statusOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="orderTable"
                :data="list"
                style="width: 100%;"

                v-loading="listLoading" border>

        <el-table-column label="序号" width="80" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="订单编号" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.outTradeNo }}</template>
        </el-table-column>
        <el-table-column label="提交时间" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.createdTime }}</template>
        </el-table-column>
        <el-table-column label="收货人" align="center">
          <template slot-scope="scope">
            <p>
              收货人： {{ scope.row.receiver }}
            </p>
            <p>
              tel：{{ scope.row.tel }}
            </p>
            <p>收货地址: {{ scope.row.address }}</p>
          </template>
        </el-table-column>

        <el-table-column label="用户订单备注" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.note }}</template>
        </el-table-column>

        <el-table-column label="订单金额" width="120" align="center">
          <template slot-scope="scope">￥{{ scope.row.price }}</template>
        </el-table-column>


        <el-table-column label="订单状态" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.status | formatStatus }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleViewOrder(scope.$index, scope.row)"
            >查看订单
            </el-button>
            <el-button
              size="mini"
              @click="handleDeliveryOrder(scope.$index, scope.row)"
              v-show="scope.row.status===2">订单发货
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>


    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15,20]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="发货快递单号填写"
      :visible.sync="dialogVisible"
      width="50%">
      <el-form ref="form" :model="expressForm" label-width="80px">
        <el-form-item label="快递公司">
          <el-select
            v-model="expressForm.expressCompany"
            filterable
            allow-create
            default-first-option
            placeholder="请选择或填写快递公司">
            <el-option
              v-for="item in company"
              :label="item" :key="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号">
          <el-input v-model="expressForm.express"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="addExpress">确定</el-button>
          <el-button @click="dialogVisible=false">取消</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>
<script>
import {expressAdd, fetchList} from '@/api/order'

const defaultExpressForm = {
  oid: null,
  express: null,
  expressCompany: '顺丰快递'
}
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  outTradeNo: null,
  state: 2,
  keyword: null,
  createTime: null,
  express: null
};
const defaultLogisticsCompanies = ["顺丰快递", "圆通快递", "中通快递", "韵达快递", "邮政快递"];
export default {
  name: "orderList",

  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      expressForm: Object.assign({}, defaultExpressForm),
      listLoading: true,
      list: null,
      total: null,
      company: Object.assign({}, defaultLogisticsCompanies),
      operateType: null,
      dialogVisible: false,
      closeOrder: {
        dialogVisible: false,
        content: null,
        orderIds: []
      },
      statusOptions: [
        {
          label: '全部',
          value: null
        },
        {
          label: '已取消',
          value: 0
        },
        {
          label: '待付款',
          value: 1
        },
        {
          label: '待发货',
          value: 2
        },
        {
          label: '待收货',
          value: 3
        },
        {
          label: '已完成',
          value: 4
        }
      ],

      logisticsDialogVisible: false
    }
  },
  created() {
    this.getList();
  },
  filters: {
    formatStatus(value) {
      if (value === 0)
        return '已取消'
      if (value === 1)
        return '待付款'
      if (value === 2)
        return '待发货'
      if (value === 3)
        return '待收货'
      return '交易完成'
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleViewOrder(index, row) {
      this.$router.push({path: '/oms/orderDetail', query: {id: row.id}})
    },
    handleCloseOrder(index, row) {
      this.closeOrder.dialogVisible = true;
      this.closeOrder.orderIds = [row.id];
    },
    handleDeliveryOrder(index, row) {
      console.log(index, row)
      this.expressForm = Object.assign({}, defaultExpressForm)
      this.expressForm.oid = row.id
      this.dialogVisible = true
    },


    handleDeleteOrder(index, row) {
      let ids = [];
      ids.push(row.id);
      this.deleteOrder(ids);
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    addExpress() {
      expressAdd(this.expressForm).then(res => {
        this.$message.success(res.message)
        this.dialogVisible = false;
        this.expressForm = Object.assign({}, defaultExpressForm)
        this.getList()
      })
    }


  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


